<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################

header('P3P: CP="NOI DSP COR NID CUR OUR NOR"');
require "config.php";

if ($_COOKIE['ccampaign'])
{

	$vcampaigns = explode("|", $_COOKIE['ccampaign']);
	$vcampaigns = array_reverse($vcampaigns);

	if ($type == "sale")
	{

		if (!preg_match("/\b\d+\.\d{2}\b/", $value))
		{
			record_alert("Revenue must be in \$0.00 format in a.php");
			header("Location: http://$domain/$udirectory/1x1.gif");
			exit;
		}

		//check if visitor already hit this
		if ($adtrackz_sales)
		{
			$cids = explode("|", $adtrackz_sales);
			foreach ($cids as $cid)
			{
				if ($cid == $id)
				{
					header("Location: http://$domain/$udirectory/1x1.gif"); 
					exit;
				}
			}
		}

		//go through each until you find the last one that tracks sales
		foreach($vcampaigns as $vcpgns)
		{
			$thiscampaign = explode("&",$vcpgns);
			$thiscampaign_purpose = mysql_fetch_row(mysql_query("select purpose from adtrackz_campaigns where adcode='$thiscampaign[0]'"));
			if ($thiscampaign_purpose[0] == "all" || $thiscampaign_purpose[0] == "sales")
			{
				$last_sales_campaign = $thiscampaign;
				break;
			}
		} //now $last_sales_campaign holds the last campaign (and/or sub) that tracked a sale


		if ($last_sales_campaign) //record sales only if a campaign with sales tracking was found
		{
			$campaignID = mysql_fetch_row(mysql_query("select ID from adtrackz_campaigns where adcode='$last_sales_campaign[0]'"));

			//get subID for subcampaign		
			if ($thiscampaign[1])
			{
				$sID = mysql_fetch_row(mysql_query("select ID from adtrackz_subcampaigns where adcode='$last_sales_campaign[1]' && campaignID='$campaignID[0]'"));

				//if not found, it means subcampaign was deleted, but it still recorded a sale
				if (!$sID[0])
				{
					record_alert("sale recorded for deleted campaign: $last_sales_campaign[0] and subcampaign:$last_sales_campaign[1], revenue=$value, id=$id, name=$name, description=$description");
					header("Location: http://$domain/$udirectory/1x1.gif"); 
					exit;
				}
			}
			$time = time();
			mysql_query("insert into adtrackz_sales values('','$campaignID[0]','$sID[0]',$time,'$value','$id','$name','$description')");
		}

		//set cookie tagging this visitor with this productID
		if ($adtrackz_sales)
			$cvalue = $adtrackz_sales."|".$id;
		else
			$cvalue = $id;			
		setcookie("adtrackz_sales",$cvalue,mktime (0,0,0,12,32,2010),"/");
	}
	elseif ($type == "action")
	{

		//check if visitor already hit this
		if ($adtrackz_actions)
		{
			$cids = explode("|", $adtrackz_actions);
			foreach ($cids as $cid)
			{
				if ($cid == $id)
				{
					header("Location: http://$domain/$udirectory/1x1.gif"); 
					exit;
				}
			}
		}

		foreach($vcampaigns as $vcpgns)
		{
			$thiscampaign = explode("&",$vcpgns);
			$thiscampaign_purpose = mysql_fetch_row(mysql_query("select purpose from adtrackz_campaigns where adcode='$thiscampaign[0]'"));
			if ($thiscampaign_purpose[0] == "all" || $thiscampaign_purpose[0] == "actions")
			{
				$last_action_campaign = $thiscampaign;
				break;
			}
		} //now $last_action_campaign holds the last campaign (and/or sub) that tracked an action

		if ($last_action_campaign)
		{

			$campaignID = mysql_fetch_row(mysql_query("select ID from adtrackz_campaigns where adcode='$last_action_campaign[0]'"));

			//get subID for this campaign
			if ($thiscampaign[1])
			{
				$sID = mysql_fetch_row(mysql_query("select ID from adtrackz_subcampaigns where adcode='$last_action_campaign[1]' && campaignID='$campaignID[0]'"));		

				//if not found, it means subcampaign was deleted, but it still recorded a sale
				if (!$sID[0])
				{
					record_alert("action recorded for deleted campaign: $last_action_campaign[0] and subcampaign:$last_action_campaign[1], id=$id, name=$name, description=$description");
					header("Location: http://$domain/$udirectory/1x1.gif"); 
					exit;
				}
			}
		}

		$time = time();
		mysql_query("insert into adtrackz_actions values('','$campaignID[0]','$sID[0]',$time,'$id','$name','$description')");

		//set cookie tagging this visitor with this productID
		if ($adtrackz_actions)
			$cvalue = $adtrackz_actions."|".$id;
		else
			$cvalue = $id;			
		setcookie("adtrackz_actions",$cvalue,mktime (0,0,0,12,32,2010),"/");
	}
	else
		record_alert("invalid type specified in a.php");	
}

header("Location: http://$domain/$udirectory/1x1.gif"); 

function record_alert($reason)
{
	mysql_query("insert into adtrackz_alerts values ('','$c','$s',now(),'$referer','$reason');");
}

?>